﻿using System;
using System.Configuration;
using System.Data.SqlClient;

namespace GeneratorVM.OnlyAuthenticated
{
    public partial class OnlyAuthenticated_EditAbrevieri : System.Web.UI.Page
    {
        private string _currentUser;
        private readonly SqlConnection _connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
    
        protected void Page_Load(object sender, EventArgs e)
        {
            _currentUser = LoginName.Page.User.Identity.Name;
            currentUserTextBox.Text = _currentUser;
            GenreHiddenTextArea.Value = MFRadioButtonList.SelectedItem.Text;
        }


        protected void InsertButton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(TextBox1.Text)
                & !TextBox1.Text.EndsWith(" ")
                & !TextBox1.Text.StartsWith(" ")
                & TextBox2.Text != string.Empty)
            {

                InsertRecord(TextBox1.Text, TextBox2.Text, DropDownListSpecialitate.SelectedValue,
                             CheckBoxBold.Checked, CheckBoxItalic.Checked, DropDownListMarimeText.SelectedValue, MFRadioButtonList.SelectedValue, _currentUser);
            }

            TextBox1.Text = string.Empty;
            TextBox2.Text = string.Empty;
            DropDownList1.DataBind(); //rebind
        }



        private void InsertRecord(string abreviere, string textAbreviere, string numeSpecialitate, Boolean bold, Boolean italic, string marimeText, string gen, string currentUser1)
        {
            string sqlStatement = "INSERT INTO Abrevieri (Abreviere, TextAbreviere, Specialitate, Bold, Italic, MarimeText, Gen, UserEditor) VALUES (@abreviere, @textAbreviere, @numeSpecialitate, @bold, @italic, @marimeText, @gen, @currentUser1)";

            try
            {
                _connection.Open();
                SqlCommand cmd = new SqlCommand(sqlStatement, _connection);
                cmd.Parameters.AddWithValue("@Abreviere", abreviere);
                cmd.Parameters.AddWithValue("@TextAbreviere", textAbreviere);
                cmd.Parameters.AddWithValue("@NumeSpecialitate", numeSpecialitate);
                cmd.Parameters.AddWithValue("@Bold", bold);
                cmd.Parameters.AddWithValue("@Italic", italic);
                cmd.Parameters.AddWithValue("@MarimeText", marimeText);
                cmd.Parameters.AddWithValue("@Gen", gen);
                cmd.Parameters.AddWithValue("@currentUser1", currentUser1);
                cmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Deletion/Insertion Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                _connection.Close();
            }

        }

        protected void MFRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
        {
            GenreHiddenTextArea.Value = MFRadioButtonList.SelectedItem.Text;
        }
    }
}